package com.xsomnus.java_basic.bitops;

/**
 * @author 叶小夏
 * @since 2019-08-19 17:54
 */
public class BitOps {

    public static boolean isOdd(int num) {
        return (num & 1) != 0;
    }

    public static boolean log2(int num) {
        return (num & (num - 1)) == 0;
    }

    public static int numberOfLeadingZeros(int i) {
        // HD, Count leading 0's
        if (i <= 0)
            return i == 0 ? 32 : 0;
        int n = 31;
        if (i >= 1 << 16) {
            n -= 16;
            i >>>= 16;
        }
        if (i >= 1 << 8) {
            n -= 8;
            i >>>= 8;
        }
        if (i >= 1 << 4) {
            n -= 4;
            i >>>= 4;
        }
        if (i >= 1 << 2) {
            n -= 2;
            i >>>= 2;
        }
        return n - (i >>> 1);
    }


    public static RGB hex2Rgb(int hexVal) {
        if (hexVal < 0 || hexVal > RGB.MAX_HEX) {
            throw new IllegalArgumentException("param is not illegal");
        }
        return new RGB((hexVal & 0xff0000) >> 16, (hexVal & 0xff00) >> 8, hexVal & 0xff);
    }

    public static String rgbToHexStr(RGB rgb) {
        return Integer.toHexString(rgbToHex(rgb));
    }

    public static int rgbToHex(RGB rgb) {
        return rgb.getRed() << 16 | rgb.getGreen() << 8 | rgb.getBlue();
    }


    public static void main(String[] args) {

        int x = 0b11000000;
        System.out.println(x);

    }


    public static int inverse(int i) {
        return ~i;
    }

    // 异或运算
    public static int xor(int i) {
        return 1 ^ i;
    }


}
